/**
 * 
 */
package liwei;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import zhangzhifan.SearchDlg;

/**
 * @author LiWei
 * 
 */
public class MyMenuBar extends JMenuBar
{

	/**
	 * 命令栏的构造函数
	 * the constructor of the Menubar
	 */
	public MyMenuBar()
	{
		JMenu fileMenu = new JMenu("File");
		JMenu editMenu = new JMenu("Edit");
		JMenu advanceMenu = new JMenu("Advance");
		add(fileMenu);
		add(editMenu);
		add(advanceMenu);
		//add the frame of converting number systems
		JMenuItem conversionItem = new JMenuItem("BinHexOctDec");
		advanceMenu.add(conversionItem);
		conversionItem.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				//open the converting frame when pressed
				ConversionFrame aConversionFrame = new ConversionFrame();
				aConversionFrame.setVisible(true);
			}
		});
		JMenuItem emailItem = new JMenuItem("send an Email");
		advanceMenu.add(emailItem);
		emailItem.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				//open a frame of Email sending and send the parameter(string) to it
				EmailFrame aEmailFrame = new EmailFrame();
				aEmailFrame.setVisible(true);
				textString = myPanel.getText();
				aEmailFrame.setText(textString);
			}
		});
		//to open a file and read it
		JMenuItem openMenuItem = new JMenuItem("Open");
		fileMenu.add(openMenuItem);
		openMenuItem.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent event)
			{
				JFileChooser fileChooser = new JFileChooser();
				int returnVal = fileChooser.showOpenDialog(
						null);
					if(returnVal == JFileChooser.APPROVE_OPTION) {
						try
						{
							myPanel.readFile(fileChooser.getSelectedFile());
						} catch (FileNotFoundException e)
						{
							// TODO 自动生成的 catch 块
							//e.printStackTrace();
							Main.logger.log(Level.ALL, "Can't read the selected file");
						}
					}
				
			}
		});
		JMenuItem saveMenuItem = new JMenuItem("Save as");
		saveMenuItem.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JFileChooser fileChooser = new JFileChooser();
				int returnVal = fileChooser.showOpenDialog(
						null);
					if(returnVal == JFileChooser.APPROVE_OPTION) {
						myPanel.writeFile(fileChooser.getSelectedFile());
					}
			}
		});
		fileMenu.add(saveMenuItem);
		JMenuItem exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				//close the program when EXIT is pressed
				System.exit(0);
			}
		});
		fileMenu.add(exitMenuItem);
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setSize(5, getHeight());
		add(helpMenu);
		//to open the helping document
		helpMenu.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{	
				  try { // using command line to start a web Browser so that we can see the "help document"
					  Runtime.getRuntime().exec(
							  "cmd /c start D:\\help.html"
							  ); 
					  } 
				  catch(IOException exception){
					  
				  }
			}
		});

		//to set the font 
		JMenuItem fontMenuItem = new JMenuItem("Font");
		fontMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO 自动生成的方法存根
				Main.fontChooseDlg.setVisible(true);
			}
		});
		editMenu.add(fontMenuItem);
		JMenuItem findAndReplaceItem = new JMenuItem("FindAndReplace");
		editMenu.add(findAndReplaceItem);
		findAndReplaceItem.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				SearchDlg searchDlg = new SearchDlg(null,myPanel.getMainTextArea());
				searchDlg.setVisible(true);
			}
		});
		JMenuItem copyMenuItem = new JMenuItem("Copy");
		editMenu.add(copyMenuItem);
		copyMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO 自动生成的方法存根
				myPanel.getMainTextArea().copy();
			}
		});
		JMenuItem cutMenuItem = new JMenuItem("cut");
		editMenu.add(cutMenuItem);
		cutMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				myPanel.getMainTextArea().cut();
			}
		});
		JMenuItem pasteMenuItem = new JMenuItem("paste");
		editMenu.add(pasteMenuItem);
		pasteMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				myPanel.getMainTextArea().paste();
			}
		});
		final JMenuItem undoMenuItem = new JMenuItem("undo");
		final JMenuItem redoMenuItem = new JMenuItem("redo");
		editMenu.add(undoMenuItem);
		undoMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO 自动生成的方法存根
				Main.undoManager.undo();
				redoMenuItem.setEnabled(Main.undoManager.canRedo());
				undoMenuItem.setEnabled(Main.undoManager.canUndo());
			}
		});
		
		editMenu.add(redoMenuItem);
		redoMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO 自动生成的方法存根
				Main.undoManager.redo();
				
				redoMenuItem.setEnabled(Main.undoManager.canRedo());
				undoMenuItem.setEnabled(Main.undoManager.canUndo());
			}
		});
		JMenuItem selectAllMenuItem = new JMenuItem("select All");
		editMenu.add(selectAllMenuItem);
		selectAllMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO 自动生成的方法存根
				myPanel.getMainTextArea().selectAll();
			}
		});
	}

	public void setMyPanel(MyPanel panel)
	{
		myPanel = panel;
	}
	
	private MyPanel myPanel;
	private String textString;
}
